<?php
session_start();
require 'connection.php';
require 'util.php';
$p = $_POST;
switch($p['action'])
{
	default:
		getLeftSide($p['city']);
}
function getLeftSide($city)
{
	$query = "SELECT result.pk, result.title, result_picture.picture_url, result_picture.cropped_picture_url, result.description, result.price, location.latitude, location.longitude, address.district, result.beds FROM result INNER JOIN address on result.address_fk = address.pk INNER JOIN location ON address.location_fk = location.pk LEFT JOIN result_picture ON result.pk = result_picture.result_fk AND result_picture.main_picture = 1  WHERE address.city = '{$city}'";
	$resultArray = getResultArray($query, $GLOBALS['pdo']);
	generateListedResultsArray($resultArray);
	$results = array();
	foreach($resultArray as $r){
		$results[] =getArrayFromResut($r);
	}

	echo json_encode($results);
}
/*
$query = "SELECT result.pk, result.title, result_picture.picture_url, result_picture.cropped_picture_url, result.description, result.price, location.latitude, location.longitude FROM result INNER JOIN address on result.address_fk = address.pk INNER JOIN location ON address.location_fk = location.pk LEFT JOIN result_picture ON result.pk = result_picture.result_fk";
$resultArray = getResultArray($query, $pdo);
$jsArray = Array();
$ids = Array();
foreach($resultArray as $r){
	$jsArray[]=getJsonScriptFromResut($r);
	$ids[] = $r['pk'];
}
?>



<?php
	foreach ($jsArray as $element) {
		echo $element;
	}
?>



<?php
	foreach ($ids as $id) {
    $hiddenPk = getHiddenListingPk($id);
		echo "resultArray[result{$hiddenPk}.id] = result{$hiddenPk};";
	}
?>


//This was in home.php



  $query = "SELECT result.pk, result.title, result.description, result.price, location.latitude, location.longitude,address.district, result.beds FROM result INNER JOIN address on result.address_fk = address.pk INNER JOIN location ON address.location_fk = location.pk ";
  $resultArray = getResultArray($query, $pdo);
  generateListedResultsArray($resultArray);



   <?php
        
        foreach ($resultArray as $r) {
          $hiddenPk = getHiddenListingPk($r['pk']);
          $title = strtoupper($r['title']);
          $imageUrl = getListingPictureNotLoggedIn($r['pk']);
          $description = $r['description'];
          //print_r($r);
          $beds = trim($r['beds']);
          $bedsHtml = $beds != "" ? "<div class=\"beds\"><span class=\"icon\"></span><span>{$beds}</span></div>" : "";
          //$bathrooms = trim($r['beds']);
          $descriptionLength = strlen($description);
          if($descriptionLength > 165)
          {
            $description = substr($description, 0, 162)."...";
          }
          $suburb = strtoupper($r['district']);
          $price = (float)$r['price'];
          echo "<div class=\"result\" id=\"$hiddenPk\">
                  <div class=\"listing-image-container\">
                    <img src=\"{$imageUrl}\"> 
                    </img>
                  </div>
                  <div class=\"result-info\">
                    <div class=\"result-info-head\">
                      <span class=\"ellipsis\">{$title}</span>
                      <span>&#163;{$price}</span>
                    </div>
                    <div class=\"result-info-suburb\">
                      <p><span class=\"bold-text\">SUBURB:</span> {$suburb}</p>
                    </div>
                    <div class=\"result-info-description\">
                      <p>{$description}</p> 
                    </div>
                    <div class=\"result-info-additional\">
                      {$bedsHtml}
                    </div>
                    <div class=\"result-info-more\">
                      <a id=\"mapLink{$hiddenPk}\" href=\"#\">MAP</a>
                      <a id=\"moreInfoButton{$hiddenPk}\" class=\"more-info-button-open\" href=\"#\"></a>
                    </div>
                  </div>
                </div>";
        }
      ?>


      function getJsonScriptFromResut($result){
	$pk = getHiddenListingPk($result['pk']);
	$title = strtoupper($result['title']);
	$imageUrl = getListingPictureNotLoggedIn($result['pk']);
	
	$description = $result['description'];
	$price = ((float)$result['price']);
	$latitude = $result['latitude'];
	$longitude = $result['longitude'];
	$jS = "var result{$pk} = {
			  id: 'r{$pk}' ,
			  divId: \"{$pk}\",
			  mapLinkId: \"mapLink{$pk}\",
			  position: {
			    latitude: {$latitude}, 
			    longitude: {$longitude}
			  },
			  iconImage: {
			    path: \"img/marker.png\",
			    height:48,
			    width:42,
			    scaledHeight:48,
			    scaledWidth:42
			  },

			  markerString: '',

			  infoWindowString: '<div class=\"infoWindow2\" id=\"infoWindow_r{$pk}\">'+
							   ' <div class=\"imageContainer\" style=\"background-image: url(\'{$imageUrl}\');\">'+
							    '</div>'+
							    '<div class=\"infoWindowContents\">'+
							     ' <div class=\"i-w-headingContainer\">'+
							      '  <p class=\"i-w-headingTitle ellipsis\">{$title}</p><p class=\"i-w-price\">&#163;{$price}</p>'+
							     ' </div>'+
							     ' <div class=\"i-w-body\">'+
							      '  <p class=\"ellipsis\">{$description}</p>'+
							     ' </div>'+
							      '<div class=\"i-w-links\">'+
							        '<a class=\"moreInfoLink\" id=\"moreInfoLinkMap_{$pk}\" href=\"#\">MORE INFO</a><a class=\"closeLink\" href=\"#\" onclick=\"window.results.closeInfoBox()\">X</a>'+
							      '</div>'+
							   ' </div>'+
							   ' <div class=\"i-w-pin\">'+
							   '   <div>'+
							        ' </div>'+
							         '<div>'+
							         '</div>'+
							       '</div>'+
							     '</div>',
			  infoWindowData:{
			    pixelOffset:{
			      x:-30,
			      y: -80
			    },
			    width:210
			  }
			};";
	return $jS;
}

      */

      ?>